<?php
include("../config/FTPConnection.inc");
include_once("constant.php");

class FTP {
	
	private static $host;
	private static $username;
	private static $password; 
	private static $instance;
	
	private function FTP(){
		self::$host = FTPConnection::getHost();
		self::$username = FTPConnection::getUsername();
		self::$password = FTPConnection::getPassword();
	}
	
	public static function getInstance() {
        if (!self::$instance)
            self::$instance = new FTP();
        return self::$instance;
    }
	
	public static function getServerPath(){
		return FTPConnection::getServerPath();
	}
	
	private function connectToHost($host, $username, $password){
		$connId = ftp_connect($host);
		$loginResult = ftp_login($connId, $username, $password);
		if ((!$connId) || (!$loginResult)) return null;
		return $connId;
	}
	
	public static function ftpMakeDir($dirPath, $mode) {    
		$connId = self::connectToHost(self::$host, self::$username, self::$password);
		if($connId != null){
			if(ftp_mkdir($connId, $dirPath)){
				ftp_chmod($connId, $mode, $dirPath); 
				ftp_quit($connId);
				return true;
			}
			return false;
		}
		return false;
	}
	
	public static function ftpUpload($remoteFile, $file, $mode){
		$connId = self::connectToHost(self::$host, self::$username, self::$password);
        //."/".extractLastDirectoryOrFileFromPath($file)
		if($connId != null){		
			if(ftp_put($connId, $remoteFile, $file, FTP_ASCII)){
				ftp_chmod($connId, $mode, $remoteFile); 
				ftp_quit($connId);
				return true;
			}
			return false;
		}
		return false;
	}
	
	public static function ftpDelete($file, $mode){
		$connId = self::connectToHost(self::$host, self::$username, self::$password);
		if($connId != null){
			if(ftp_delete($connId, $file)){
				ftp_quit($connId);
				return true;
			}
			return false;
		}
		return false;
	}
    
    public static function ftpDeleteDir($dir){
        $connId = self::connectToHost(self::$host, self::$username, self::$password);
        if($connId != null){
			if(ftp_rmdir($connId, $dir)){
				ftp_quit($connId);
				return true;
			}
			return false;
		}
		return false;
    }
    
    public static function ftpRename($oldName, $newName){
        $connId = self::connectToHost(self::$host, self::$username, self::$password);
        if($connId != null){
			if(ftp_rename($connId, $oldName, $newName)){
				ftp_quit($connId);
				return true;
			}
			return false;
		}
		return false;
    }
    
}
?>